from modelscope import AutoModelForCausalLM, AutoTokenizer

model_name = "D:\\models\\Qwen\\Qwen3-1.7B"

# load the tokenizer and the model
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
    model_name,
    torch_dtype="auto",
    device_map="cpu"
)

# prepare the model input
prompt = "你是待办提醒助手,帮我识别用户输入是否是一个待办或提醒,以{\"code\": \"true或false\",\"response\": {\"desc\": \"输入的原文\",\"time\": \"提醒时间\"}},模板样例{\"code\": \"true\",\"response\": {\"desc\": \"明天晚上8点给客户打电话\",\"time\": \"明天晚上8点\"}},返回严格的json格式,不要有其它多余字符。以下是用户输入:开会"
messages = [
    {"role": "system", "content": "你是智能助手！"},
    {"role": "user", "content": prompt}
]
text = tokenizer.apply_chat_template(
    messages,
    tokenize=False,
    add_generation_prompt=True,
    # enable_thinking=True # Switches between thinking and non-thinking modes. Default is True.
    enable_thinking=False # Switches between thinking and non-thinking modes. Default is True.
)
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)

# conduct text completion
generated_ids = model.generate(
    **model_inputs,
    # max_new_tokens=32768
    max_new_tokens=4096
)
output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()

# parsing thinking content
try:
    # rindex finding 151668 (</think>)
    index = len(output_ids) - output_ids[::-1].index(151668)
except ValueError:
    index = 0

thinking_content = tokenizer.decode(output_ids[:index], skip_special_tokens=True).strip("\n")
content = tokenizer.decode(output_ids[index:], skip_special_tokens=True).strip("\n")

print("thinking content:", thinking_content)
print("content:", content)